package com.wip.reportingclient.services.impls;

import java.io.ByteArrayOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import com.wip.reportingclient.services.interfaces.IWriterService;

@Service
public class WriterServiceImpl implements IWriterService {

	//private static Logger logger = Logger.getLogger("service");
	 /**
	  * Writes the report to the output stream
	  */
	 public void write(HttpServletResponse response, HSSFSheet worksheet) {
	   
	  //logger.debug("Writing report to the stream");
	  try {
	   // Retrieve the output stream
	   ServletOutputStream outputStream = response.getOutputStream();
	   // Write to the output stream
	   worksheet.getWorkbook().write(outputStream);
	   // Flush the stream
	   outputStream.flush();
	 
	  } catch (Exception e) {
	   //logger.error("Unable to write report to the output stream");
	  }
	 }


	 public void write(HttpServletResponse response, HSSFWorkbook workbook) {

			// logger.debug("Writing report to the stream");
			try {
				// 6. Set the response properties
				String fileName = workbook.getSheetName(0)+".xls";
				response.setHeader("Content-Disposition", "inline; filename="
						+ fileName);
				// Make sure to set the correct content type
				response.setContentType("application/vnd.ms-excel");

				// Retrieve the output stream
				ServletOutputStream outputStream = response.getOutputStream();
				// Write to the output stream
				workbook.write(outputStream);
				// Flush the stream
				outputStream.flush();

			} catch (Exception e) {
				// logger.error("Unable to write report to the output stream");
			}
		}
	 
	 public void write(HttpServletResponse response, ByteArrayOutputStream baos, String fileName) {

			// logger.debug("Writing report to the stream");
			try {
				// 6. Set the response properties
				//String fileName = workbook.getSheetName(0)+".xls";
				response.setHeader("Content-Disposition", "inline; filename="
						+ "Performance-"+fileName);
				// Make sure to set the correct content type
				response.setContentType("application/vnd.ms-excel");

				// Retrieve the output stream
				ServletOutputStream outputStream = response.getOutputStream();
				// Write to the output stream
				baos.writeTo(outputStream);
				// Flush the stream
				outputStream.flush();

			} catch (Exception e) {
				// logger.error("Unable to write report to the output stream");
			}
		}

	 
	 public void writePdf(HttpServletResponse response, ByteArrayOutputStream baos, String fileName) {

			// logger.debug("Writing report to the stream");
			try {
				// setting some response headers
				response.setContentType("application/pdf");
				response.setHeader("Content-Disposition",
						"attachment; filename=Performance-"+fileName);
				response.setContentLength(baos.size());

				// Retrieve the output stream
				ServletOutputStream outputStream = response.getOutputStream();
				// Write to the output stream
				baos.writeTo(outputStream);
				// Flush the stream
				outputStream.flush();
				
				// write ByteArrayOutputStream to the ServletOutputStream
				/*OutputStream os = response.getOutputStream();
				baos.writeTo(os);
				os.flush();
				os.close();*/

			} catch (Exception e) {
				// logger.error("Unable to write report to the output stream");
			}
		}

}
